﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Elsa.Workflows;
using Elsa.Workflows.Models;
using Newtonsoft.Json;
using T.FAS.Runtime.MF.Entity;
using T.FAS.Runtime.MF.Entity.ElsaEntity;

namespace T.FAS.Runtime.MF.Activities.ActivityInputOutput
{
    public class FasStartNodeExecuteDetailHandler : AbsActivityExecuteDetailHandler
    {
        public FasStartNodeExecuteDetailHandler(
           IActivity activity,
           ActivityExecutionRecord activityExecutionRecord,
           IEnumerable<ResolvedVariable> runtimeVariables,
           WorkflowInstance workflowInstance) : base(activity, activityExecutionRecord, runtimeVariables, workflowInstance)
        {

        }

        public override WorkflowInstanceDetail GetInputOutput()
        {
            var inputs = new Dictionary<string, object>();
            var outputs = new Dictionary<string, object>();
            var detail = new WorkflowInstanceDetail();

            var startActivity = Activity as FasStartActivity;

            if (!string.IsNullOrEmpty(ActivityExecutionRecord.SerializedActivityState))
            {
                var javaScriptActivity = JsonConvert.DeserializeObject<FasStartActivity>(ActivityExecutionRecord.SerializedActivityState);
                foreach (var param in javaScriptActivity.InputInstanceValues ?? new Dictionary<string, object>())
                {
                    inputs.Add(param.Key, param.Value);
                }

                foreach (var variable in javaScriptActivity.OutputInstanceValues ?? new Dictionary<string, object>())
                {
                    outputs.Add(variable.Key, variable.Value);
                }

            }
            detail.Inputs = inputs;
            detail.Outputs = outputs;
            return detail;
        }
    }
}
